<!--
 * @Author: qwlbwm 18370818801qwl./
 * @Date: 2023-09-06 13:36:58
 * @LastEditors: whereamazinghappen 306702547@qq.com
 * @LastEditTime: 2023-09-23 15:56:25
 * @FilePath: \checc-business-module-web-kgpt-dev\src\views\examineManage\components\editAddStandardIndex.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 通用的评分管理（包括中交和中咨） -->
  <div>
    <el-dialog
      v-model="dialogVisible"
      :show-close="false"
      width="1100px"
      :before-close="beforeClose"
      title="分解明细"
    >
      <div class="examine-dialog-content">
        <el-row style="padding-bottom: 10px" v-if="detailData">
          <el-col :span="8">指标：{{ detailData.indexName }}</el-col>
          <el-col :span="8">单位：{{ detailData.companyName }}</el-col>
          <el-col :span="8">计划贡献：{{ detailData.score }}</el-col>
        </el-row>
        <div v-if="currentScoreCompose">
          <el-tabs v-model="tabActive" @tab-click="tabClick">
            <el-tab-pane
              :label="item.composeItem_text"
              v-for="item in scoreComposeArr"
              :key="item.id"
              :name="item.id"
            ></el-tab-pane>
          </el-tabs>

          <div v-if="currentScoreCompose.scoreRule !== '4'">
            <el-table
              :data="currentScoreCompose.recordList"
              style="width: 100%"
              row-key="id"
              border
              :span-method="arraySpanMethod"
              height="300px"
            >
              <el-table-column type="index" align="center" width="80" label="序号" />
              <el-table-column prop="name" align="center" width="120" label="名称" />
              <el-table-column prop="datasource" align="center" label="类别" />
              <el-table-column prop="joinValue_text" align="center" label="参与方式" />
              <el-table-column prop="rankValue_text" align="center" label="排名">
              </el-table-column>
              <el-table-column prop="planCompleteNum" align="center" label="计划完成数量">
              </el-table-column>
              <el-table-column prop="score" align="center" label="得分">
              </el-table-column>
              <el-table-column prop="sumScore" label="合计" align="center">
                <template #default>
                  {{ currentScoreCompose.sumScore || 0 }}
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div v-else>自评分：{{ currentScoreCompose.selfScore }}</div>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, reactive, watch, toRefs } from "vue";
import { dataDictionary } from "@/api/dict";
import * as api from "../../../api/zJexaminePlan";
export default defineComponent({
  emits: {
    "update:visible": null,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true, // 必传项
    },
    handleRow: {
      //处理的指标数据
      type: Object,
    },
  },
  setup(props, { emit }) {
    const dialogVisible = computed(() => {
      return props.visible;
    });
    const dict_datas = reactive({
      dict_ranklist: [],
      dict_joinMethods: [],
    });
    const state = reactive<any>({
      //整体数据带dict全是字典查询
      scoreComposeArr: [],
      currentScoreCompose: null, //当前tab选中的组成详情
      tabActive: "",
      handleItemRow: null, //处理的配置项数据
      table_list: [],
      opType: "add", //操作方式 add/edit
      detailData: null,
    });
    const beforeClose = () => {
      cancel();
    };
    const tabClick = (TabsPaneContext) => {
      let tab = state.scoreComposeArr[TabsPaneContext.index],
        sumScore = "";
      if (tab.recordList && tab.recordList.length) {
        sumScore = tab.recordList.reduce((c, u) => c + (u.score || 0), 0);
      }
      state.currentScoreCompose = state.scoreComposeArr[TabsPaneContext.index];
      state.currentScoreCompose.sumScore = sumScore;
      //   state.tabActive = TabsPaneContext.props.name
    };
    const cancel = () => {
      emit("update:visible", false);
    };
    const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
      let alength = state.currentScoreCompose.recordList
        ? state.currentScoreCompose.recordList.length
        : 0;
      if (columnIndex == 7) {
        if (rowIndex == 0) {
          return [alength, 1];
        } else {
          return [0, 0];
        }
      }
    };
    const filterRankFactor = (rank) => {
      let str = "";
      if (rank) {
        for (const key in rank) {
          let a: any = dict_datas.dict_ranklist.find(
            (item: any) => item.itemValue === key
          );
          str += a.itemText + `(${rank[key]})`;
        }
      }
      return str;
    };
    const filterJoinMethod = (mt) => {
      let str = "";
      if (mt) {
        for (const key in mt) {
          let a: any = dict_datas.dict_joinMethods.find(
            (item: any) => item.itemValue === key
          );
          str += a.itemText + `(${mt[key]})`;
        }
      }
      return str;
    };
    watch(
      () => props.visible,
      (newval) => {
        if (newval) {
          getScoreDetail();
        }
      }
    );
    const refresh = () => {
      getScoreDetail(state.tabActive);
    };
    const getScoreDetail = (active?) => {
      //获取评分管理详情列表
      api.degradeDetailRequest(props.handleRow as any).then((res) => {
        let response = res.data.data;
        state.detailData = response;

        if (response.cellResponseList && response.cellResponseList.length) {
          state.scoreComposeArr = response.cellResponseList;
          if (active) {
            //如果已经在其他tab了就自动切换
            state.currentScoreCompose = response.cellResponseList.find(
              (item) => item.id === active
            );
          } else {
            state.currentScoreCompose = response.cellResponseList[0];
          }
          state.tabActive = state.currentScoreCompose.id;
        }
      });
    };
    const getDictDatas = () => {
      let dictIds = ["KHGL_COMPOSE_SET_RANK_CODE", "KHGL_COMPOSE_SET_JOIN_METHOD_CODE"];

      dataDictionary(dictIds).then((res) => {
        let response = res.data.data;
        dict_datas.dict_ranklist = response.KHGL_COMPOSE_SET_RANK_CODE;
        dict_datas.dict_joinMethods = response.KHGL_COMPOSE_SET_JOIN_METHOD_CODE;
      });
    };
    getDictDatas();

    return {
      ...toRefs(state),
      dict_datas,
      dialogVisible,
      arraySpanMethod,
      beforeClose,
      filterRankFactor,
      filterJoinMethod,
      refresh,
      tabClick,
      cancel,
      confirm,
    };
  },
});
</script>
<style lang="scss" scoped>
.examine-dialog-content {
  padding: 20px;
}
</style>
